package com.yilin.tms.truck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.tms.core.commons.model.AuthStatus;
import com.yilin.tms.core.entity.data.statistics.truck.VehicleStatistics;
import com.yilin.tms.core.entity.truck.TransportLicense;
import com.yilin.tms.core.entity.truck.TravelLicense;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.truck.service.ITruckStatisticsService;
import com.yilin.tms.truck.mapper.TransportLicenseMapper;
import com.yilin.tms.truck.mapper.TravelLicenseMapper;
import com.yilin.tms.truck.mapper.TruckStatisticsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.*;


/**
 * 车辆服务
 *
 * @author zhangjinyuan
 * @date 2022/10/24
 */
@Service
@Transactional
public class TruckStatisticsService  implements ITruckStatisticsService {

    @Resource
    TruckStatisticsMapper statisticsMapper;
    @Resource
    TravelLicenseMapper travelLicenseMapper;
    @Resource
    TransportLicenseMapper transportLicenseMapper;

    /**
     * 统计不同状态的车辆
     */
    @Override
    public Map<String, Object> getVehicleByTruckStatus() {
        return statisticsMapper.getVehicleByTruckStatus();
    }

    /**
     * 认证条数
     * */
    @Override
    public Integer getTravelLicenseNoAuthCount() {
        QueryWrapper<TravelLicense> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("auth_status", AuthStatus.waitProcess);
        queryWrapper.eq("is_deleted",0);
        return Math.toIntExact(travelLicenseMapper.selectCount(queryWrapper));
    }

    @Override
    public Integer getTransportLicenseNoAuthCount() {
        QueryWrapper<TransportLicense> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("auth_status", AuthStatus.waitProcess);
        queryWrapper.eq("is_deleted",0);
        return Math.toIntExact(transportLicenseMapper.selectCount(queryWrapper));
    }


    /**
     * 获取车源统计数据
     */
    @Override
    public VehicleStatistics getVehicleStatistics(Account loginUser) {
        //车辆总计、年新增、月新增、日新增计数
        VehicleStatistics vehicleStatistics = statisticsMapper.getVehicleCount(loginUser);
        //车辆4.5和4.5以下对比饼图
        vehicleStatistics.setVehicleWeightCake(statisticsMapper.getVehicleWeightCake(loginUser));
        //车辆状态分布饼图
        vehicleStatistics.setVehicleStatusCake(statisticsMapper.getVehicleStatusCake(loginUser));
        //新增车辆趋势折线图
        vehicleStatistics.setVehicleAddTrends(statisticsMapper.getVehicleAddTrends(loginUser));
        return vehicleStatistics;
    }

}
